package io.geekidea.boot.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.test.dto.IssueFeedbackDto;
import io.geekidea.boot.test.entity.IssueFeedback;
import io.geekidea.boot.test.mapper.IssueFeedbackMapper;
import io.geekidea.boot.test.query.IssueFeedbackQuery;
import io.geekidea.boot.test.service.IssueFeedbackService;
import io.geekidea.boot.test.vo.IssueFeedbackVo;
import io.geekidea.boot.test.query.AppIssueFeedbackQuery;
import io.geekidea.boot.test.vo.AppIssueFeedbackVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 问题反馈 服务实现类
 *
 * @author geekidea
 * @since 2025-03-26
 */
@Slf4j
@Service
public class IssueFeedbackServiceImpl extends ServiceImpl<IssueFeedbackMapper, IssueFeedback> implements IssueFeedbackService {

    @Autowired
    private IssueFeedbackMapper issueFeedbackMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addIssueFeedback(IssueFeedbackDto dto) {
        IssueFeedback issueFeedback = new IssueFeedback();
        BeanUtils.copyProperties(dto, issueFeedback);
        return save(issueFeedback);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateIssueFeedback(IssueFeedbackDto dto) {
        Long id = dto.getId();
        IssueFeedback issueFeedback = getById(id);
        if (issueFeedback == null) {
            throw new BusinessException("问题反馈不存在");
        }
        BeanUtils.copyProperties(dto, issueFeedback);
        return updateById(issueFeedback);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteIssueFeedback(Long id) {
        return removeById(id);
    }

    @Override
    public IssueFeedbackVo getIssueFeedbackById(Long id) {
        return issueFeedbackMapper.getIssueFeedbackById(id);
    }

    @Override
    public Paging<IssueFeedbackVo> getIssueFeedbackPage(IssueFeedbackQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<IssueFeedbackVo> list = issueFeedbackMapper.getIssueFeedbackPage(query);
        Paging<IssueFeedbackVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppIssueFeedbackVo getAppIssueFeedbackById(Long id) {
        return issueFeedbackMapper.getAppIssueFeedbackById(id);
    }

    @Override
    public Paging<AppIssueFeedbackVo> getAppIssueFeedbackPage(AppIssueFeedbackQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppIssueFeedbackVo> list = issueFeedbackMapper.getAppIssueFeedbackPage(query);
        Paging<AppIssueFeedbackVo> paging = new Paging<>(list);
        return paging;
    }

}
